package com.atech.ace.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atech.ace.business.client.OrderAdministrationRemote;
import com.atech.ace.entity.Category;

/**
 * Servlet implementation class CatalogController
 */
public class CatalogController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private Context context;
	@EJB
	private OrderAdministrationRemote admin;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CatalogController() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		
	}



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nextPage = "";
		try {
			Map catalog = getCompleteCatalog();
			System.out.println(catalog.size());
			request.setAttribute("catalog", catalog);
			if(request.getSession().getAttribute("user") == null)
			{
				nextPage = "/previewcatalog.jsp";
			}
			else
			{
				nextPage = "/store/catalog.jsp";
			}
			
			
			getServletConfig().getServletContext().getRequestDispatcher(nextPage).forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	
	private Map getCompleteCatalog() 
	 throws Exception
	{
		
		Map catalog = admin.getCatalog();
		return catalog;
	}
	
	private List getCatalog(Category category)
	throws Exception
	{
		
		List catalog = admin.getProducts(category);
		return catalog;
	}
	
	

}
